package visao;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JToolBar;
import javax.swing.JMenuBar;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import javax.swing.JButton;

public class FrmPrincipal extends JFrame {

	private JPanel contentPane;
	private JMenuBar menuPrincipal;
	private JMenu menuSair;
	private JMenu menuCadastro;
	private JMenuItem itemPessoa;
	private JMenuItem itemContato;
	private JMenuItem itemSair;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FrmPrincipal frame = new FrmPrincipal();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public FrmPrincipal() {

		inicializarComponentes();
		acaoMenus();
	}

	private void inicializarComponentes() {

		setTitle("Agenda");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);

		// Centralizar o JFrame ao executar o programa
		this.setLocationRelativeTo(null);

		// Menu Principal
		menuPrincipal = new JMenuBar();
		setJMenuBar(menuPrincipal);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		// Menu Cadastro
		menuCadastro = new JMenu("Cadastro");
		menuPrincipal.add(menuCadastro);

		itemPessoa = new JMenuItem("Pessoa");
		menuCadastro.add(itemPessoa);

		// Menu Sair
		menuSair = new JMenu("Sair da Agenda");
		menuPrincipal.add(menuSair);

		itemSair = new JMenuItem("Sair");
		menuSair.add(itemSair);

	}

	private void acaoMenus() {
		clickMenuSair();
		clickItemPessoa();
	}

	private void clickItemPessoa() {

		itemPessoa.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					PessoaTableModelTest CadastroPessoa = new PessoaTableModelTest();
					CadastroPessoa.setVisible(true);
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
	}

	private void clickMenuSair() {
		itemSair.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				this.sair();
			}

			private void sair() {
				sairDaAgenda();
			}
		});
	}

	private void sairDaAgenda() {
		this.dispose();
	}

}
